package com.lmf.mock.starter.autoconfigure.controller;

import com.lmf.mock.starter.autoconfigure.entity.ProxyGroup;
import com.lmf.mock.starter.autoconfigure.service.ProxyGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理分组管理控制器
 */
@RestController
@RequestMapping("/proxy/group")
public class ProxyGroupController {

    @Autowired
    private ProxyGroupService proxyGroupService;

    /**
     * 获取所有代理分组
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProxyGroup> groups = proxyGroupService.list();
            result.put("success", true);
            result.put("data", groups);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取代理分组列表失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取代理分组详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyGroup group = proxyGroupService.getGroupById(id);
            if (group != null) {
                result.put("success", true);
                result.put("data", group);
            } else {
                result.put("success", false);
                result.put("message", "代理分组不存在");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取代理分组详情失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 创建代理分组
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> create(@RequestBody ProxyGroup group) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyGroup created = proxyGroupService.createGroup(group);
            result.put("success", true);
            result.put("data", created);
            result.put("message", "代理分组创建成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "创建代理分组失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 更新代理分组
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> update(@RequestBody ProxyGroup group) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyGroup updated = proxyGroupService.updateGroup(group);
            result.put("success", true);
            result.put("data", updated);
            result.put("message", "代理分组更新成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新代理分组失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 删除代理分组
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = proxyGroupService.deleteGroup(id);
            if (deleted) {
                result.put("success", true);
                result.put("message", "代理分组删除成功");
            } else {
                result.put("success", false);
                result.put("message", "代理分组删除失败");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除代理分组失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 切换代理分组状态
     */
    @PostMapping("/{id}/toggle")
    public ResponseEntity<Map<String, Object>> toggleStatus(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean toggled = proxyGroupService.toggleGroupStatus(id);
            if (toggled) {
                result.put("success", true);
                result.put("message", "代理分组状态切换成功");
            } else {
                result.put("success", false);
                result.put("message", "代理分组状态切换失败");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "切换代理分组状态失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}
